package com.yz.xyt.service.impl;

import com.yz.xyt.domain.Dorm;
import com.yz.xyt.mapper.DormMapper;
import com.yz.xyt.service.IDormService;
import com.yz.xyt.utils.DateUtils;
import com.yz.xyt.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DormServiceImpl implements IDormService {

    @Autowired
    DormMapper dormMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return dormMapper.deleteByPrimaryKey(id);
    }

    @Transactional
    @Override
    public int insert(Dorm record) {
        return dormMapper.insert(record);
    }

    @Transactional
    @Override
    public int insertSelective(Dorm record) {
        return dormMapper.insertSelective(record);
    }

    @Override
    public Dorm selectByPrimaryKey(Integer id) {
        return dormMapper.selectByPrimaryKey(id);
    }

    @Override
    public Dorm selectByDormNumber(String dormNumber) {
        return dormMapper.selectByDormNumber(dormNumber);
    }

    @Transactional
    @Override
    public int updateByPrimaryKeySelective(Dorm record) {
        return dormMapper.updateByPrimaryKeySelective(record);
    }

    @Transactional
    @Override
    public int updateByDormNumberSelective(Dorm record) {
        return dormMapper.updateByDormNumberSelective(record);
    }

    @Transactional
    @Override
    public int updateByPrimaryKey(Dorm record) {
        return dormMapper.updateByPrimaryKey(record);
    }

    /**
     * 增量添加
     * @param list
     */
    @Transactional
    public void syncAddDorm(List<Dorm> list){
        //过滤重复数据
        List<Dorm> dormList = new ArrayList<>();
        list.stream().forEach(dorm -> {
            Dorm record = selectByDormNumber(dorm.getDormNumber());
            if(record == null){
                dormList.add(dorm);
            }
        });
        //新增班级信息
        dormList.stream().peek(dorm -> {
            dorm.setCreateBy(SecurityUtils.getUsername());
            dorm.setCreateDate(DateUtils.getNowDate());
        }).collect(Collectors.toList()).stream().forEach(this::insertSelective);

    }

    /**
     * 同步更新
     * @param list
     */
    @Transactional
    @Override
    public void syncUpdateDorm(List<Dorm> list) {
        //更新班级信息
        list.stream().peek(dorm -> {
            dorm.setUpdateBy(SecurityUtils.getUsername());
            dorm.setUpdateDate(DateUtils.getNowDate());
        }).collect(Collectors.toList()).forEach(this::updateByDormNumberSelective);

    }

    /**
     * 同步删除
     * @param list
     */
    @Transactional
    @Override
    public void syncDeleteDorm(List<Dorm> list) {
        list.stream().forEach(dorm -> {
            dormMapper.deleteByDormNumber(dorm.getDormNumber());
        });
    }

    /**
     * 添加宿舍信息
     * @param dorm
     * @return
     */
    @Override
    public int addDorm(Dorm dorm) {
        dorm.setCreateBy(SecurityUtils.getUsername());
        dorm.setCreateDate(DateUtils.getNowDate());
        return dormMapper.insertSelective(dorm);
    }
}
